var fs = require('fs')
var path = require('path')
function copy(src, dst) {
    console.log('copy file ' + src + ' to ' + dst);
    fs.createReadStream(src).pipe(fs.createWriteStream(dst));
}
//创建多层文件夹 同步
function mkdirsSync(dirpath, mode) {
    if (!fs.existsSync(dirpath)) {
        var pathtmp;
        dirpath.split(path.sep).forEach(function (dirname) {
            if (pathtmp) {
                pathtmp = path.join(pathtmp, dirname);
            }
            else {
                pathtmp = dirname;
            }
            if (!fs.existsSync(pathtmp)) {
                if (!fs.mkdirSync(pathtmp, mode)) {
                    return false;
                }
            }
        });
    }
    return true;
}
var exec = require('child_process').exec;
exec('npm run build', function (err, stdout, stderr) {
    console.log(stdout)
    if (err) {
        console.log('build fail:' + stderr);
    } else {
        copy('index.html', '../www/index.html')
        fs.readdir("./static", function (err, files) {
            if (err) {
                return console.error(err);
            }
            mkdirsSync('../www/static/');

            files.forEach(function (file) {
                copy('./static/' + file, '../www/static/' + file)
            });
            console.log('copy over');
        });
    }
});